package com.example.qjia.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.example.qjia.Listener.RegisterListener;
import com.example.qjia.R;
import com.example.qjia.basic.BaseActivity;
import com.example.qjia.model.RegisterModel;
import com.example.qjia.model.RegisterModelImp;
import com.example.qjia.ui.ViewInterface.RegisterViewInterFace;

public class RegisterActivity extends BaseActivity implements View.OnClickListener,RegisterViewInterFace ,RegisterListener {
    private EditText studentName;
    private EditText password;
    private EditText surePassword;
    private EditText telNumber;
    private EditText studentId;
    private EditText studentmail;
    private EditText studentmaster;
    private EditText masterClass;
    private EditText academy;



    private Button registerBtn;
    private RegisterModel registerModel;
    private ProgressBar loading;
    private LinearLayout back_ly;
    private static final String TAG="RegisterActivity";



    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_register;
    }

    @Override
    protected void init() {
        registerModel=new RegisterModelImp();
        studentName=(EditText)findViewById(R.id.student_name);

        password=(EditText)findViewById(R.id.password);
        surePassword=(EditText)findViewById(R.id.sure_password);
        studentId=(EditText)findViewById(R.id.studentId);
        telNumber=(EditText)findViewById(R.id.tel);
        studentmail=(EditText)findViewById(R.id.studentmail);
        studentmaster=(EditText)findViewById(R.id.master);
        masterClass=(EditText)findViewById(R.id.m_class);
        academy=(EditText)findViewById(R.id.academy);

        loading=(ProgressBar)findViewById(R.id.loading);
        back_ly=(LinearLayout)findViewById(R.id.back_ly);
        back_ly.setOnClickListener(this);
        registerBtn=(Button)findViewById(R.id.btn_register);
        registerBtn.setOnClickListener(this);
    }

    @Override
    protected void initView() {

    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.btn_register){
            String name=studentName.getText().toString();
            String pwd=password.getText().toString();
            String s_pwd=surePassword.getText().toString();
            String s_id= studentId.getText().toString();
            String tel=telNumber.getText().toString();
            String master=studentmaster.getText().toString();
            String Acadamy=academy.getText().toString();
            String m_class=masterClass.getText().toString();
            String mail=studentmail.getText().toString();
            System.out.println(mail+"-"+pwd+"-"+s_pwd);

            if(!name.equals("")&&!pwd.equals("")&&!s_pwd.equals("")&&!s_id.equals("")&&!tel.equals("")&&!master.equals("")&&!Acadamy.equals("")&&!m_class.equals("")&&!mail.equals("")) {
                if(pwd.equals(s_pwd)){
                showProgressBar();
                registerModel.registerNewUser(name, pwd, s_id, tel, master, m_class, Acadamy, mail, this);
                }
                else {
                    toast("两次密码不一致");
                }
            }
            else {
                toast("请先把信息补充完整");
            }
        }
        if(view.getId()==R.id.back_ly) backToMain();


    }

    @Override
    public void toLogin() {
        startActivity(new Intent(RegisterActivity.this,LoginActivity.class));

    }

    @Override
    public void showProgressBar() {
        registerBtn.setVisibility(View.INVISIBLE);
        loading.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgressBar() {

        loading.setVisibility(View.INVISIBLE);
        registerBtn.setVisibility(View.VISIBLE);
    }

    @Override
    public void showRegisterError() {

    }

    @Override
    public void registerSuccess() {

        this.toLogin();
        hideProgressBar();

    }
    @Override
    public void backToMain(){
        startActivity(new Intent(this,LoginActivity.class));
        finish();
    }
    @Override
    public void registerFailed() {
        hideProgressBar();
        toast("注册失败了");

    }
}
